package utstat.listeners;

import java.util.HashMap;
import java.util.Map;

import utstat.UtEvent;
import utstat.UtEventListener;
import static utstat.Type.*;
import static utstat.BodyPart.*;
import static utstat.listeners.HeadShotListener.DammageType.*;

public class HeadShotListener implements UtEventListener {
	
	/**
	 *  undammaged + hit = dammaged
	 *    headshot + hit = dammaged
	 */
	public enum DammageType { undammaged,dammaged,headshot; }
    private Map<String, Integer> headshotsGiven = new HashMap<String, Integer>();
    private Map<String, DammageType> dammage = new HashMap<String, DammageType>();
	private int currentRound = -1;
    
    @Override
    public void processEvent(UtEvent e) {
    	
    	if(e.getRound()!=currentRound) {nextRound(); currentRound =e.getRound();}
    	
    	String src = e.getSrc();
    	String dest = e.getDest();
    	if(e.getType()==kill) {
    		DammageType currentDestDammage = dammage.get(dest);
    		
    	//	System.out.println(e.getDest()+" was killed "+currentDestDammage);
    		if(currentDestDammage==headshot) {
    			increment(e,headshotsGiven);
    		//	System.out.println("one shot kill "+headshotsGiven.get(e.getSrc())+" by "+e.getSrc()+" with a "+e.getWeapon());
    			}
    		dammage.put(dest, undammaged);
    		
    		Integer destHeadshots = headshotsGiven.get(dest);
    		if(destHeadshots!=null&&destHeadshots>2) System.out.println(dest+" died after dealing "+destHeadshots+" headshots");
    		headshotsGiven.put(dest, 0); 		
    		
    	}
    	if(e.getType()==hit)  {
    		DammageType currentDestDammage = dammage.get(dest);
     //       System.out.println(e.getDest()+" was hit "+currentDestDammage);
            
            if(currentDestDammage!=headshot 
            && currentDestDammage!=dammaged 
              && (e.getBodyPart()==Head 
              || e.getBodyPart()==Helmet)) 
            	{ dammage.put(dest, headshot); }
            else dammage.put(dest, dammaged);
        }
    }

	@Override
	public String getIntestingFact(String player) {
		return null;
	}
	
	private int increment(UtEvent e, Map<String, Integer> countMap) {
		Integer count = countMap.get(e.getSrc());
		if(count==null) count=0;
		count++;
		countMap.put(e.getSrc(),count);
		return count;
	}
	
	private void nextRound() {
		headshotsGiven= new HashMap<String, Integer>();
		 dammage = new HashMap<String, DammageType>();
	}
}
